package com.luoluo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;

public class PDFParser implements Parser {

	@Override
	public String parse(InputStream in) {
		try {
			StringWriter writer = new StringWriter();
			PdfReader reader = new PdfReader(in);
			int num = reader.getNumberOfPages();
			String content = null;
			for (int i = 1; i < num; i++) {
				content = PdfTextExtractor.getTextFromPage(reader, i);
				writer.write(content);
			}
			writer.flush();
			return writer.getBuffer().toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		PDFParser parser = new PDFParser();
		try {
			System.out.println(parser.parse(new FileInputStream(new File(
					"D:/tmp/1.pdf"))));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
